<?php
namespace app\admin\logic;

use think\Model;
use app\admin\model\NewsModel;
use app\admin\model\NewsLabelModel;
use app\admin\model\ClassModel;
use app\admin\model\LabelModel;

class NewsLogic extends Model
{

	// 获取文章分类
	public function get_news_class()
	{
		$where = ['status' => 1];
		$order = "class_id asc";
		$field = "*";
		// 查询所有分类
		$class = new ClassModel();
		$res   = $class->get_list($where, $order, $field);

		return $res;
	}

	// 获取文章标签
	public function get_news_label()
	{
		$where = ['status' => 1];
		$order = "label_id asc";
		$field = "*";
		// 查询所有分类
		$label = new LabelModel();
		$res   = $label->get_list($where, $order, $field);

		return $res;
	}

	/**
	 * 数据操作
	 * @param [array]  $data [文章信息]
	 * @param [string] $flag [操作类型]
	 */
	public function add_news($data, $flag)
	{
		$news      = new NewsModel();
		$newslabel = new NewsLabelModel();

		// 接收参数
		$param = [
			'title'    => $data['title'],
			'keywords' => $data['keywords'],
			'desc'     => $data['desc'],
			'class_id' => $data['class_id'],
			'label_id' => $data['label_id'],
			'image'    => $data['image'],
			'content_html'  => $data['container-html-code'],
            'content'  => $data['test-editormd-markdown-doc'],
			'content_md'  => $data['content_md'],
			'ordernum' => $data['ordernum'],
			'scan'     => $data['scan'],
			'status'   => $data['status'],
			'top'      => $data['top'],
			'home'     => $data['home'],
		];

		$param['top']  = empty($data['top'])? -1: $data['top'];
		$param['home'] = empty($data['home'])? -1: $data['home'];

		if ($flag == "add") {
			$res = $news->insert_data($param);
			if (isset($res['msg'])) return $res;

			// 标签关联
			if (!empty($data['label_id'])) {
				$labels = explode(",", $data['label_id']);

				$cond = [];
				foreach ($labels as $k => $v) {
					$arr = [
						'label_id' => $v,
						'news_id'  => $res,
					];

					$cond[] = $arr;
				}
			}

			// 批量添加
			$res = $newslabel->edit_data_bantch($cond);
			if (count($res) <= 0) {
				return msg_error("标签添加失败！");
			}

			return msg_success("添加成功！");

		}elseif($flag == "edit"){

			$param['news_id'] = $data['news_id'];
			$res = $news->save_data($param);
			if (isset($res['msg'])) return $res;

			// 获取原标签
			$arr = $newslabel->get_list(['news_id' => $data['news_id']]);
			$label_old = [];
			foreach ($arr as $k => $v) {
				$label_old[] = $v['label_id'];
			}

			// 标签操作
			if (!empty($data['label_id'])) {
				$label_new = explode(",", $data['label_id']);

				//新增标签
				$add_arr = array_diff($label_new, $label_old);
				if (!empty($add_arr)) {
					$batchs  = [];
					foreach ($add_arr as $k => $v) {
						$cond = [
							'label_id' => $v,
							'news_id'  => $data['news_id'],
						];

						$batchs[] = $cond;
					}
					$res = $newslabel->edit_data_bantch($batchs);// 批量添加
					if (count($res) <= 0) {
						return msg_error("标签添加失败！");
					}					
				}

				//删除标签
				$del_arr = array_diff($label_old, $label_new);
				if (!empty($del_arr)) {
					$where = "news_id = ".$data['news_id']." AND label_id IN (".implode(",", $del_arr).")";
					$res   = $newslabel->del_data($where);
					if (isset($res['msg'])) {
						return msg_error("标签删除失败！");
					}

				}

			}

			return msg_success("编辑成功！");
		}
	}

	/**
	 * 获取文章列表
	 * @param  [type] $title [description]
	 * @param  [type] $class [description]
	 * @param  [type] $label [description]
	 * @param  [type] $order [description]
	 * @param  [type] $start [description]
	 * @param  [type] $size  [description]
	 * @return [type]        [description]
	 */
	public function get_news_list($title, $class, $label, $status, $order, $start, $size)
	{
		$news 	   = new NewsModel();
		$newslabel = new NewsLabelModel();

		$where = [];
		if (!empty($status)) {
			$where[] = "status = ".$status;
		}else{
			$where[] = "status < 2";
		}

		if (!empty($title)) {
			$where[] = "title LIKE '%".$title."%'";
		}
		if (!empty($class)) {
			$where[] = "class_id = ".$class;
		}
		if (!empty($label)) {
			$labels = $newslabel->get_list(['label_id' => $label]);
			if (!empty($labels)) {
				$arr    = [];
				foreach ($labels as $k => $v) {
					$arr[] = $v['news_id'];
				}
				$where[] = "(news_id IN (".implode(",", $arr)."))";
			}
		}

		if (count($where) > 0) {
			$wherearr = implode(" AND ", $where);
		}else{
			$wherearr = '';
		}

		// 数量
		$sum  = $news->get_count($wherearr);
		// 列表
		$list = $news->get_list_page($wherearr, $order, $fields = "*", $start, $size);
		// 获取分类
		$classes = $this->get_news_class();
		foreach ($list as $k => $v) {
			// 分类名称
			foreach ($classes as $ke => $va) {
				if ($v['class_id'] == $va['class_id']) {
					$list[$k]['classname'] = $va['name'];
				}
			}
		}

		$res  = [
			'sum'  => $sum,
			'list' => $list,
		];

		return $res;
	}
}